package Majisuka
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.charts.BubbleChart;
	import mx.controls.Image;
	import mx.core.UIComponent;
	
	import spark.components.BorderContainer;
	import spark.components.Button;

	public class Place
	{
		private var _name:String ;
		private var _x:int ;
		private var _y:int ;
		private var _id:int ;
		private var _owner:int ;
		private var _neigh:Vector.<int> ;
		private var _img:String ; // The background when battle happens in this place
		private var _graph:Bitmap ; // The bitmap class for background
		private var _icon:Button ; // The icon for the place on the map. So far, it is a button
		
		public function Place()
		{
			_neigh = new Vector.<int> ;
			_icon = new Button() ;
		}

		public function get graph():Bitmap
		{
			return _graph;
		}

		public function set graph(value:Bitmap):void
		{
			_graph = value;
		}

		public function get neigh():Vector.<int>
		{
			return _neigh;
		}

		public function set neigh(value:Vector.<int>):void
		{
			_neigh = value;
		}

		public function get name():String
		{
			return _name;
		}

		public function set name(value:String):void
		{
			_name = value;
		}

		public function get owner():int
		{
			return _owner;
		}

		public function set owner(value:int):void
		{
			_owner = value;
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		public function get y():int
		{
			return _y;
		}

		public function set y(value:int):void
		{
			_y = value;
		}

		public function get x():int
		{
			return _x;
		}

		public function set x(value:int):void
		{
			_x = value;
		}
		
		public function Save():XML
		{
			// Generate a XML object which contains the information of this player 
			var x:XML ;
			var s:String = "" ;
			var i:int ;
			var len:int ;
			
			len = neigh.length ;
			for ( i = 0 ; i < len ; ++i )
				s = s + _neigh[i].toString() + " " ;
			
			x = <place pid={_id}>
					<name>{_name}</name>
					<x>{_x}</x>
					<y>{_y}</y>
					<img>{_img}</img>
					<owner>{_owner}</owner>
					<neigh>{s}</neigh>
				</place> ;
			return x ;
		}
		
		public function Load( id:int, xml:XML ):void
		{
			// Load item "id" from the xml file
			var s:String ;
			var i:int ;
			var j:int ;
			var v:int ;
			var len:int ;
			
			_id = id ;
			_name = xml.name ;
			_x = int(xml.x) ;
			_y = int(xml.y) ;
			_img = xml.img ;
			_owner = int(xml.owner ) ;
			s = xml.neigh ;
			
			v = 0 ;
			len = s.length ;
			for ( i = 0 ; i < len ; ++i )
			{
				if ( s.charAt(i) < "0" || s.charAt(i) > "9" )
				{
					_neigh.push( v ) ;
					v = 0 ;
					continue ;
				}
				
				v = v * 10 + ( int( s.charAt(i) ) - int( "0".charAt(0) ) ) ;
			}
			_neigh.push( v ) ;
			
			// Load the image
			if ( _img != "0" )
			{
				var loader:Loader = new Loader() ;
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnLoadImageComplete ) ;
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, OnLoadImageError ) ;
				loader.load( new URLRequest( "image\\" + _img ) ) ;
			}
			else
			{
				_graph = new Bitmap() ;
				var data:BitmapData = new BitmapData( 100, 100, false, 0xffffff ) ;
				_graph.bitmapData = data ;
			}
		}
		
		public function Draw( currPlace:int, back:BorderContainer ):Button
		{
			var pm:Image = new Image() ; // An flag beside the button indicating the owner
			
			_icon.height = 30 ;
			if ( _id == currPlace )
				_icon.emphasized = true ;
			else
				_icon.emphasized = false ;
			_icon.label =  _name ;
			_icon.x = _x ;
			_icon.y = _y ;
			_icon.id = _id.toString() ;
			//pb.addEventListener(MouseEvent.CLICK, PlaceClick ) ;
				
			// Attach the bitmap to the button
			var colors:Array = new Array( 0x00ff00, 0xff0000, 0x0000ff,
										0x808000, 0x800080, 0x008080 ) ; // So far, we offer 6 groups
			
			var bitmapData:BitmapData = new BitmapData( 5, 30, false, colors[ _owner ] ) ;
			var bitmap:Bitmap = new Bitmap( bitmapData ) ;
			bitmap.x = bitmap.y = 0 ;
			//var ui:UIComponent = new UIComponent() ;
			//ui.addChild( bitmap ) ;
			pm.addChild( bitmap ) ;
			pm.x = _x - 5 ;
			pm.y = _y ;
			back.addElement( pm ) ;
			back.addElement( _icon ) ;			
			return _icon ;
		}
		
		public function Select():void
		{
			_icon.emphasized = true ;
		}
		
		public function Unselect():void
		{
			_icon.emphasized = false ;
		}
		
		public function OnLoadImageComplete( evt:Event ):void
		{
			_graph = new Bitmap() ;
			var data:BitmapData = evt.target.content.bitmapData ;
			_graph.bitmapData = data ;
		}
		
		public function OnLoadImageError( evt:ErrorEvent ):void
		{
			_graph = new Bitmap() ;
			var data:BitmapData = new BitmapData( 100, 100, false, 0xffffff ) ;
			_graph.bitmapData = data ;
		}
		
		public function GetBattleFieldBackground( width:int, height:int ):Bitmap
		{
			if ( _graph == null )
				return null ;
			_graph.width = width ;
			_graph.height = height ;
			return _graph ;
		}
	}
}